import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

// 组件的引入
import Login from '@/views/login.vue'
import Personal from '@/views/personal/index.vue'
import EditPersonal from '@/views/personal/editPersonal.vue'
import Index from '@/views/index.vue'
import Register from '@/views/register.vue'
import ArticleDetail from '@/views/articleDetail.vue'
import MyStars from '@/views/myStars.vue'
import Comment from '@/views/comment.vue'

// 创建路由模块对象
const router = new VueRouter({
    // 添加路由配置
    routes: [
        // 添加路由默认重定向
        {
            path: '/',
            redirect: { name: 'index.js' }
        },
        // 添加具体的路由配置:实现路由映射组件
        {
            name: 'login',
            path: '/login',
            component: Login
        },
        {
            name: 'register',
            path: '/register',
            component: Register
        },
        {
            name: 'personal',
            path: '/personal/:id',
            component: Personal
        },
        {
            name: 'editPersonal',
            path: '/editPersonal/:id',
            component: EditPersonal
        },
        {
            name: 'index',
            path: '/index',
            component: Index
        },
        {
            name: 'ArticleDetail',
            path: '/articleDetail/:id',
            component: ArticleDetail
        },
        {
            name: 'MyFollows',
            path: '/myFollows',
            component: MyFollows
        },
        {
            name: 'MyStars',
            path: '/myStars',
            component: MyStars
        },
        {
            name: 'Comment',
            path: '/comment/:id',
            component: Comment
        }


    ]
})

// 添加导航守卫
router.beforeEach((to, from, next) => {
    console.log('aabbcc')
        // 当前的跳转是否跳转到需要进行授权认证的页面，如/personal
    if (to.name == 'personal') {
        let token = localStorage.getItem('hm_toutiao_52')
        if (token) {
            next()
        } else {
            next({ path: '/login' })
        }
    } else {
        next()
    }
})

// 暴露
export default router